package com.website.travle.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.website.travle.mapper.ISpotDAOImpl;
import com.website.travle.pojo.Spot;
import com.website.travle.pojo.User;
import com.website.travle.utils.ApiResp;
import com.website.travle.utils.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@WebServlet("/api/spot")
public class SpotController extends HttpServlet {
    private static ISpotDAOImpl spotDAO = new ISpotDAOImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        switch (operation) {
            case "add":
                try {
                    add(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "delete":
                try {
                    delete(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "update":
                try {
                    update(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "query":
                try {
                    query(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "getOne":
                try {
                    getOne(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            default:
                error(req, resp);
                break;

        }
    }

    private void getOne(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        ResultSet resultSet = JdbcUtils.executeQuery("select * from spot where id = ?", req.getParameter("id"));
        Spot spot = new Spot();
        while (resultSet.next()) {
            spot.setId(resultSet.getInt("id"));
            spot.setName(resultSet.getString("name"));
            spot.setIntroduction(resultSet.getString("introduction"));
            spot.setImageUrl(resultSet.getString("image_url"));
            spot.setCreateTime(resultSet.getString("create_time"));
        }
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(new ApiResp<>(0, "success", 1, spot));
        // 设置响应内容类型为JSON
        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(json);
        resp.getWriter().flush();
    }

    private void error(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.getWriter().write("operation error!!!");
    }

    private void query(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        List<Spot> spots = spotDAO.pageByLikeName(Integer.valueOf(page), Integer.valueOf(limit), name);
        // 创建Gson对象
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        // 将Person对象转换为JSON字符串
        String json = gson.toJson(new ApiResp<>(0, "success", spotDAO.getTotal(), spots));
        // 设置响应内容类型为JSON
        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(json);
        resp.getWriter().flush();
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String introduction = req.getParameter("introduction");
        String imageUrl = req.getParameter("imageUrl");
        Spot spot = new Spot(Integer.valueOf(id), name, introduction, imageUrl, new Date().toString());
        int update = spotDAO.update(spot);
        if (update == 1) {
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            String json = gson.toJson(new ApiResp<>().successfully());
            resp.setContentType("application/json;charset=UTF-8");
            resp.getWriter().write(json);
            resp.getWriter().flush();
        } else {
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            String json = gson.toJson(new ApiResp<>().failed("更新失败"));
            resp.setContentType("application/json;charset=UTF-8");
            resp.getWriter().write(json);
            resp.getWriter().flush();
        }


    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        spotDAO.delete(Integer.valueOf(id));
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(new ApiResp<>().successfully());
        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(json);
        resp.getWriter().flush();
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        String name = req.getParameter("name");
        String introduction = req.getParameter("introduction");
        String imageUrl = req.getParameter("imageUrl");
        System.out.println(name + introduction + imageUrl);
        Spot spot = new Spot(null, name, introduction, imageUrl, new Date().toString());
        System.out.println("spotadd" + spot);
        int add = spotDAO.add(spot);
        if (add == 1) {
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            String json = gson.toJson(new ApiResp<>().successfully());
            resp.setContentType("application/json;charset=UTF-8");
            resp.getWriter().write(json);
            resp.getWriter().flush();
        } else {
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            String json = gson.toJson(new ApiResp<>().failed("添加失败"));
            resp.setContentType("application/json;charset=UTF-8");
            resp.getWriter().write(json);
            resp.getWriter().flush();
        }

    }


}
